﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// MultiSelectHandler 的摘要说明
    /// </summary>
    public class MultiProblemHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            string type = context.Request.Params["type"];
            OnLineExam.BLL.MultiProblem bll = new OnLineExam.BLL.MultiProblem();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);
                    string depid = context.Request.Params["depid"];
                    int startIndex = (page - 1) * rows + 1;
                    int endIndex = (page) * rows;

                    //var loginuser=new UserManager().GetModel(context.User.Identity.Name);
                    var ds = bll.GetListByPage( "depid=" + depid, "ID", startIndex, endIndex);
                    int RecordCount = bll.GetRecordCount("depid=" + depid);
                    var s = HHG.Common.Json.DataTable2Json(ds.Tables[0], RecordCount);
                     
                    var r = new Regex("\r\n");
                    s = r.Replace(s," ");

                    context.Response.Write(s);
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息
                    var ID = context.Request.Params["ID"];
                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        m.ExamTime = DateTime.Now;

                        var rsult = HHG.Common.Json.GetJson<OnLineExam.Model.MultiProblem>(m);
                        context.Response.Write(rsult);
                    }

                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    //ID = context.Request.Params["ID"];
                    //var o_model = bll.GetModel(int.Parse(ID));
                    var new_model = new OnLineExam.Model.MultiProblem();
                    //new_model.ID = int.Parse(ID);
                    new_model.DepID =int.Parse( depid);
                    new_model.CourseID =0;// int.Parse(context.Request.Params["selectCourse"]);
                    new_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
                    new_model.Title = context.Request.Params["txtTitle"].Trim();
                    new_model.AnswerA = context.Request.Params["txtA"].Trim();
                    new_model.AnswerB = context.Request.Params["txtB"].Trim();
                    new_model.AnswerC = context.Request.Params["txtC"].Trim();
                    new_model.AnswerD = context.Request.Params["txtD"].Trim();
                    new_model.AnswerE = context.Request.Params["txtE"].Trim();
                    new_model.AnswerF = context.Request.Params["txtF"].Trim();
                    new_model.AnswerG = context.Request.Params["txtG"].Trim();
                    new_model.Answer = context.Request.Params["txtAnswer"].Trim();
                     

                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//修改成功，返回ID,失败，返回0
                     
                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    ID = context.Request.Params["hdID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    new_model = new OnLineExam.Model.MultiProblem();
                    new_model.ID = int.Parse(ID);
                    new_model.DepID = o_model.DepID;
                    //new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    new_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
                    new_model.Title = context.Request.Params["txtTitle"];
                    new_model.AnswerA = context.Request.Params["txtA"];
                    new_model.AnswerB = context.Request.Params["txtB"];
                    new_model.AnswerC = context.Request.Params["txtC"];
                    new_model.AnswerD = context.Request.Params["txtD"];
                    new_model.AnswerE = context.Request.Params["txtE"];
                    new_model.AnswerF = context.Request.Params["txtF"];
                    new_model.AnswerG = context.Request.Params["txtG"];
                    new_model.Answer =  context.Request.Params["txtAnswer"].ToUpper();

                    if (bll.Update(new_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}